package com.example.boxueguapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.boxueguapplication.model.User;

public class ChangeInfoActivity extends AppCompatActivity {

    private RelativeLayout mAppBar;
    private ImageView mBack;
    private TextView mTitleBar;
    private TextView save;
    private ImageView close;
    private EditText change;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nike);
        findView();
        Intent data = getIntent();
        String title = data.getStringExtra("title");
        mTitleBar.setText(title);
        mBack.setOnClickListener(v -> finish());
        save = new TextView(this);
        save.setText("保存");
        save.setTextColor(0xFFFFFFFF);
        save.setTextSize(20);
        save.setOnClickListener(view -> {
            SharedPreferences userinfo = getSharedPreferences("userinfo", MODE_PRIVATE);
            String username = userinfo.getString("LoginUser", null);
            if (!TextUtils.isEmpty(username)){
                User user = User.find(User.class, "username = ?",username).get(0);
                if (title.equals("昵称")){
                    user.setNike(change.getText().toString());
                    user.save();
                    Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent();
                    intent.putExtra("Change", change.getText().toString());
                    setResult(3,intent);
                }else if (title.equals("签名")){
                    user.setQianming(change.getText().toString());
                    user.save();
                    Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent();
                    intent.putExtra("Change", change.getText().toString());
                    setResult(4,intent);
                }
                finish();
            }
        });
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
        save.setLayoutParams(layoutParams);
        mAppBar.addView(save);
        close.setOnClickListener(view -> change.setText(""));
    }

    private void findView() {
        mAppBar = findViewById(R.id.app_bar);
        mBack = findViewById(R.id.back);
        mTitleBar = findViewById(R.id.title_bar);
        close = findViewById(R.id.close);
        change = findViewById(R.id.Change);
    }
}